//
//  MenuListTableViewCell_Table.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/8/31.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MenuListTableViewCell_Table.h"
#import "MenuListTableViewCell.h"
@implementation MenuListTableViewCell_Table

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.iconImageView fixedSize:CGSizeMake(30, 30)];
        [self.contentView addSubview:self.iconImageView];
        //
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.numberOfLines = 0;
        [self.contentView addSubview:self.titleLabel];

        self.expendButton = [[MKUIFlowLayoutButton alloc] init];
        [self.expendButton setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
        [self.expendButton setTitle:@"^" forState:UIControlStateNormal];
        [self.expendButton setTitle:@"v" forState:UIControlStateSelected];
        [self.expendButton addTarget:self action:@selector(__onExpendButtonChange:) forControlEvents:UIControlEventTouchUpInside];
        MKUILayoutConstraintItemWrapper *w3 = [MKUILayoutConstraintItemWrapper wrapItem:self.expendButton fixedSize:CGSizeMake(44, 44)];
        [self.contentView addSubview:self.expendButton];
//
        self.childrenMenuView = [[MKUITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
//        self.childrenMenuView.contentInset = UIEdgeInsetsMake(5, 10, 15, 20);
        self.childrenMenuView.layer.borderColor = [UIColor grayColor].CGColor;
        self.childrenMenuView.layer.borderWidth = 1;
        self.childrenMenuView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.childrenMenuView mk_hiddenFooterAreaSeparators];
        
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
        headView.backgroundColor = [UIColor redColor];
        headView.layer.borderColor = [UIColor grayColor].CGColor;
        headView.layer.borderWidth = 1;
        
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
        footView.backgroundColor = [UIColor redColor];
        footView.layer.borderColor = [UIColor grayColor].CGColor;
        footView.layer.borderWidth = 1;
        self.childrenMenuView.tableHeaderView = headView;
        self.childrenMenuView.tableFooterView = footView;
        
        self.childrenMenuView.bounces = NO;
        [self.contentView addSubview:self.childrenMenuView];
        //
        
        MKUIFlowLayoutConstraint *flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,self.titleLabel,w3] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsZero interitemSpacing:10];
        self.childflowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[flowlayout,self.childrenMenuView] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:UIEdgeInsetsMake(10, 20, 10, 20) interitemSpacing:10];
        //
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.childflowlayout.bounds = bounds;
    [self.childflowlayout layoutItemsWithResizeItems:YES];
}
- (void)__onExpendButtonChange:(id)sender{
    MenuListTableViewCellModel *cm = self.menuListCellModel;
    cm.expendChildren = !cm.expendChildren;
    [cm refreshWithAnimated:YES];
}
- (MenuListTableViewCellModel *)menuListCellModel{
    return (MenuListTableViewCellModel *)self.customCellModel;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    Menu *modelObject = self.customCellModel.modelObject;
    self.iconImageView.image = modelObject.icon;
    self.titleLabel.text = modelObject.title;
    
    MenuListTableViewCellModel *cm = self.menuListCellModel;
    self.expendButton.selected = cm.expendChildren;
    self.childrenMenuView.hidden = !cm.expendChildren;
    if(cm.expendChildren){
        [self reloadData];
    }
}
- (void)reloadData{
    [self.childrenMenuView.model removeAllSectionModels];
    {
        MKUITableViewSectionModel *sm = [[MKUITableViewSectionModel alloc] init];
        sm.headTitle = @"这是一个非常长的标题！！！这是一个非常长的标题！！！";
        sm.headTitle = @"11111111111111111111111111111111111111111111111111";
        sm.showHeadView = YES;
        sm.showDefaultHeadView = YES;
        sm.footTitle = @"这是一个非常长的标题！！！这是一个非常长的标题";
        sm.showFootView = YES;
        sm.showDefaultFootView = YES;
        [self.childrenMenuView.model addSectionModel:sm];
        for (Menu *menu in self.menuListCellModel.childrenMenus) {
            MenuTableViewCellModel *cm = [[MenuTableViewCellModel alloc] initWithModelObject:menu];
            cm.cellClass = [MenuSubTableViewCell class];
            [self.childrenMenuView.model addCellModel:cm];
        }
    }
    {
        MKUITableViewSectionModel *sm = [[MKUITableViewSectionModel alloc] init];
        sm.headTitle = nil;
        sm.showHeadView = YES;
        sm.showDefaultHeadView = YES;
        sm.footTitle = nil;
        sm.showFootView = YES;
        sm.showDefaultFootView = YES;
        [self.childrenMenuView.model addSectionModel:sm];
        for (Menu *menu in self.menuListCellModel.childrenMenus) {
            MenuTableViewCellModel *cm = [[MenuTableViewCellModel alloc] initWithModelObject:menu];
            cm.cellClass = [MenuSubTableViewCell class];
            [self.childrenMenuView.model addCellModel:cm];
        }
    }

    {

        MKUITableViewSectionModel *sm = [[MKUITableViewSectionModel alloc] init];
        sm.headTitle = @"这是一个非常长的Head！！！这是一个非常长的Head！！！";
        sm.showHeadView = YES;
        sm.headViewClass = [MKUITableViewSectionAdjustsView class];
        sm.footTitle = @"Foot";
        sm.showFootView = YES;
        [self.childrenMenuView.model addSectionModel:sm];
        for (Menu *menu in self.menuListCellModel.childrenMenus) {
            MenuTableViewCellModel *cm = [[MenuTableViewCellModel alloc] initWithModelObject:menu];
            cm.cellClass = [MenuSubTableViewCell class];
            [self.childrenMenuView.model addCellModel:cm];
        }

    }
    [self.childrenMenuView.model reloadTableViewData];
}
- (CGSize)customSizeThatFits:(CGSize)size{//动态高度
    return [self.childflowlayout sizeThatFits:size resizeItems:YES];
}
@end
